#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct point{
    int x,y,v;
};

void solve(){
    int n, k;
    cin >> n >> k;
    
    vector<point> points(n);
    set<int> xx, yy;
    
    for (int i = 0; i < n; i++) {
        cin >> points[i].x >> points[i].y >> points[i].v;
        xx.insert(points[i].x);
        yy.insert(points[i].y);
    }
    
    vector<int> xl(xx.begin(), xx.end());
    vector<int> yl(yy.begin(), yy.end());
    unordered_map<int, int> mpx, mpy;
    for (int i = 0; i < xl.size(); i++) {
        mpx[xl[i]] = i + 1;
    }
    for (int i = 0; i < yl.size(); i++) {
        mpy[yl[i]] = i + 1;
    }
    int aa = xl.size();
    int bb = yl.size();
    
    vector<vector<ll>> val(aa + 2, vector<ll>(bb + 2, 0));
    for (auto &p : points) {
        int x = mpx[p.x];
        int y = mpy[p.y];
        val[x][y] += p.v;
    }
    
    vector<vector<ll>> pre(aa + 2, vector<ll>(bb + 2, 0));
    for (int x = 1; x <= aa; x++) {
        ll r = 0;
        for (int y = 1; y <= bb; y++) {
            r += val[x][y];
            pre[x][y] = pre[x - 1][y] + r;
        }
    }
    
    ll res = 0;
    
    for (int i = 0; i < aa; i++) {
        for (int j = i; j < aa; j++) { 
            int a = xl[i];
            int br = xl[j];
            int di = (br + 1) - a; 
            if (di <= 0) continue; 
            
            if (di > k) continue; 
            int mx = k / di;
            if (mx <= 0) continue; 
            
            for (int p = 0; p < bb; p++) {
                for (int q = p; q < bb; q++) { 
                    int c = yl[p];
                    int dr = yl[q];
                    int h = (dr + 1) - c; 
                    if (h <= 0) continue;
                    
                    if (1LL * di * h > k) continue;
                    
                    int x1 = mpx[a]; 
                    int x2 = mpx[br];
                    int y1 = mpy[c]; 
                    int y2 = mpy[dr];
                    
                    ll temp = pre[x2][y2] - pre[x1 - 1][y2] - pre[x2][y1 - 1] + pre[x1 - 1][y1 - 1];
                    res=max(res,temp);
                }
            }
        }
    }
    
    cout << res << '\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}